package com.wpf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wpf.system.entity.UserRole;
import com.wpf.system.service.UserRoleService;
import com.wpf.system.mapper.UserRoleMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author ferna
* @description 针对表【user_role(用户关联角色表)】的数据库操作Service实现
* @createDate 2022-12-15 22:31:16
*/
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService{

    @Override
    public int deleteUserRoleByRid(Long rid) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserRole::getRid,rid);
        return this.baseMapper.delete(queryWrapper);
    }

    @Override
    public int deleteUserRoleByUid(Long uid) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserRole::getUid,uid);
        return this.baseMapper.delete(queryWrapper);
    }

    @Override
    public List<UserRole> getUserRoleByUid(Long uid) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUid,uid));
    }

    @Override
    public int saveUserRole(Long uid, List<Long> ridList) {
        //删除原有角色
        this.baseMapper.delete(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUid,uid));
        if(ridList.size()>0){
            return this.baseMapper.saveUserRole(uid,ridList);
        }else{
            return 1;
        }
    }

}




